package com.kecheng.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.kecheng.dao.PostDao;
import org.apache.struts2.ServletActionContext;
import com.kecheng.pojo.Post;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 后台文章模块的增删改查
 * @author yunxin
 *
 */
@ParentPackage("json-default")
@Action(value = "post", results = {
		@Result(name = "biaoqing", location = "/w/biaoqing.jsp"),
		@Result(name = "post", location = "/admin/post/post.jsp"),
		@Result(name = "all", location = "/admin/post/allpost.jsp"),
		@Result(name = "all11", location = "/w/post.jsp"),
		@Result(name = "id", location = "/w/perpost.jsp"),
		@Result(name = "update", location = "/admin/post/update.jsp"),
		@Result(name = "all1",type = "json",params={"root","posts"}),
})
@AllowedMethods(value = {("regex:.*")})
public class PostAction extends BaseAction{

	private File file;
	private String fileFileName;
	private String fileContentType;
	private String msg;
	@Autowired
	private PostDao postDao;
	private Post post;
	private List<Post> posts;
	private int id1;

	private String sid;
	private int[] id;

	


	/**
	 * 跳转添加页面
	 * @return
	 */
	public String toAdd(){
		return "post";
	}

	/**
	 * 添加
	 * @return
	 * @throws IOException
	 */
	public String add() throws IOException {

		String root = ServletActionContext.getServletContext().getRealPath(
				"/upload");
		InputStream is = new FileInputStream(file);
		OutputStream os = new FileOutputStream(new File(root, fileFileName));
		byte[] buffer = new byte[500];
		int length = 0;
		while (-1 != (length = is.read(buffer, 0, buffer.length))) {
			os.write(buffer);
		}

		os.close();
		is.close();

		post.setImg("upload/" + fileFileName);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// �������ڸ�ʽ

		post.setTime(df.format(new Date()));

		try {
			postDao.add(post);
			msg = "成功";

		} catch (Exception e) {
			e.printStackTrace();
			msg = "失败";
		}
		return "post";
	}

	/**
	 * 列表页
	 * @return
	 */
	public String findall() {
		if (page == null) {
			page = 1;
		}
		try {
			posts = postDao.findall(page);
			allpage = postDao.findallpage();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "all";
	}

	public String deletebyid() {

		for (int i = 0; i < id.length; i++) {
			try {
				post = postDao.findbyid(id[i]);
				postDao.delete(post);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return findall();
	}

	public String findbyid() {
		post = postDao.findbyid(id1);
		return "id";
	}

	public String deleteone() {
		postDao.delete(post);
		if (page == null) {
			page = 1;
		}
		try {
			posts = postDao.findall(page);
			allpage = postDao.findallpage();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "all";
	}



	public String findall1() {
		if (page == null) {
			page = 1;
			posts = postDao.findall(page);
			return "all11";
		} else {
			posts = postDao.findall(page);
			return "all1";
		}
	}

	public String update() {

		post = postDao.findbyid(id1);

		return "update";
	}

	@Transactional
	public String update1() throws IOException {
		System.out.println(file);
		if (file == null) {
			Post post1 = postDao.findbyid(post.getId());
			post.setImg(post1.getImg());
			post.setTime(post1.getTime());
			postDao.update(post);
			post1.setContent(post.getContent());
			if (page == null) {
				page = 1;
			}
			try {
				posts = postDao.findall(page);
				allpage = postDao.findallpage();
			} catch (Exception e) {
				e.printStackTrace();
			}

		} else {

			String root = ServletActionContext.getServletContext().getRealPath(
					"/upload");
			InputStream is = new FileInputStream(file);
			OutputStream os = new FileOutputStream(new File(root, fileFileName));
			byte[] buffer = new byte[500];
			int length = 0;
			while (-1 != (length = is.read(buffer, 0, buffer.length))) {
				os.write(buffer);
			}

			os.close();
			is.close();

			post.setImg("upload/" + fileFileName);

			Post post1 = postDao.findbyid(post.getId());

			post.setTime(post1.getTime());

			postDao.update(post);

			if (page == null || "".equals(page)) {
				page = 1;
			}
			try {
				posts = postDao.findall(page);
				allpage = postDao.findallpage();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if("tuiguang".equals(msg) || "jieshao".equals(msg)|| "jifen".equals(msg)|| "bangzhu".equals(msg)){
			return"update";
		}else{
			return "all";
		}

	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Post getPost() {
		return post;
	}

	public void setPost(Post post) {
		this.post = post;
	}

	public List<Post> getPosts() {
		return posts;
	}

	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}

	public int getId1() {
		return id1;
	}

	public void setId1(int id1) {
		this.id1 = id1;
	}


	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public int[] getId() {
		return id;
	}

	public void setId(int[] id) {
		this.id = id;
	}



}
